﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToPi.Data;
using LinqToPi;
using System.Data.OleDb;
using System.Data.Common;
using System.IO;

namespace LinqToPi
{
    public class PiPointContext : PiContext
    {
        public IQueryableTable<PipointSummary> PointSummaries { get; set; }
        public IUpdatableTable<PiPoint> Points { get; set; }

          #region Constructors
        public PiPointContext(string servername, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ;",
                                                                            PiOleDBProvider,
                                                                            servername
                                                                          )), 
                                       log)
        {               
        }

        public PiPointContext(string servername, string username, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ; User ID = {2} ; ",
                                                                          PiOleDBProvider,
                                                                          servername,
                                                                          username
                                                                          )),
                                        log)
            
        {
        }

        public PiPointContext(string servername, string username, string password, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ; User ID = {2} ; Password = {3} ; ",
                                                                            PiOleDBProvider,
                                                                            servername,
                                                                            username,
                                                                            password
                                                                            )),
                                        log)
        {
        }

        private PiPointContext(DbConnection connection, TextWriter log)
            : this(new DbQueryProvider(connection, StandardPolicy, log))
        {
            connection.Open();
        }

        public PiPointContext(IQueryProvider provider) 
        {
            this.provider = provider;
            this.PointSummaries = new QueryableTable<PipointSummary>(provider, "pipoint2");
            this.Points = new UpdatableTable<PiPoint>(provider, "pipoint2");
           //this.SnapShots = new QueryableTable<PiValue>(provider, "PiSnapshot");
           // this.Users = new UpdatableTable<PiUser>(provider, "PiUser");
           // this.UserGroups = new QueryableTable<PiGroup>(provider);
           // this.UserMemberships = new QueryableTable<PiMembership>(provider);
           // this.ProductVersions = new QueryableTable<PiProductVersion>(provider);
           // this.ArchiveData = new UpdatableTable<PiValue>(provider, "Picomp2");
          //  this.MessageLog = new UpdatableTable<PiLogItem>(provider, "PiMessageLog");

            //this.MinValues = new QueryableTable<PiMinBoundryValue>(provider, "pimin");
            //this.MaxValues = new QueryableTable<PiMaxBoundryValue>(provider, "pimax");
            //this.AverageValues = new QueryableTable<PiAverageValue>(provider, "piavg");


        }

        #endregion
    }
}
